/*
 * @Author: YWY
 * @Date: 2022-02-22 16:17:42
 * @LastEditors: YWY
 * @LastEditTime: 2022-03-06 19:16:08
 */
import { defineStore } from "pinia";
import { getSectionList, getSectionArticleList } from "../api/services/user.service";

export const sectionStore = defineStore('section', {
    state: () => {
        return {
            articleList: [],
            sectionList: [],
            currentPageCount: 1,
            currentPageList: [],
            finished: false,
        }
    },
    actions: {
        // 获取板块列表
        async loadSectionList() {
            let result = await getSectionList();
            let data = result.result;
            let arr = [];
            for (let key in data) {
                arr.push(data[key])
            }
            this.sectionList = arr
        },

        //切换板块或刷新页面获取文章列表
        async initData(id) {
            this.$patch({
                articleList: [],
                currentPageCount: 1,
                currentPageList: [],
                finished: false
            })
            console.log("在pinia内", id);
            let result = await getSectionArticleList(id, 10, this.currentPageCount);
            let data = result.result
            this.currentPageList = data.article_list
            if (this.currentPageList.length) {
                console.log('获取文章列表成功')
                let temp = this.articleList.concat(this.currentPageList)
                let temp1 = this.currentPageCount + 1
                this.$patch({
                    articleList: temp,
                    currentPageCount: temp1
                })
                return
            }
            console.log('没有更多文章了')
            this.finished = true
        },

        //上拉加载
        async loadArticleList(id) {
            let result = await getSectionArticleList(id, 10, this.currentPageCount);
            let data = result.result
            this.currentPageList = data.article_list
            if (this.currentPageList.length) {
                console.log('获取文章列表成功')
                let temp = this.articleList.concat(this.currentPageList)
                let temp1 = this.currentPageCount + 1
                this.$patch({
                    articleList: temp,
                    currentPageCount: temp1
                })
                return
            }
            console.log('获取文章列表成功')
            this.finished = true
        },
    }
})